# this class represents a view on the market
# ie a probability given for each asset to strike

class View:

	# we do nothing when we build
	def __init__(self,assets=[], probas=[]):
		if( len(assets) != len(probas) ):
			self.viewOnAssets = []
			return
		self.viewOnAssets = [(assets[ a ], probas[a]) for a in range(len(assets))]
		self.viewOnAssets.sort()
		if( len(assets) != len(probas) ):
			self.viewOnAssets = []
			return
		self.viewOnAssets = [(assets[ p ], probas[ p ]) for p in range(0,len(assets))]
		return

	# this is a builder of a view from a strategy
	def setViewFromStrategy(self, strategy):
		self.viewOnAssets = [(p.asset, p.assetPercentageInStrategy) for p in strategy.assetpercentageinstrategy_set.all()]

	# accessor
	def getFullView(self):
		return self.viewOnAssets

	# return the probability of the specified asset in the view 
	def getViewOnAsset(self, asset):
		if self.viewOnAssets == []:
			return 0
		for elt in self.viewOnAssets:
			# asset are identified by their name
			if elt[0].name == asset.name:
				return elt[1]
		return 0
        
	# in order to serialize the view to JSON we have a function that can convert it to a 
	# dictionary
	def toDictionary(self):
		return { 'viewList':[{ 'assetName':d[0].name, 'view':d[1]} for d in self.getFullView()]}
